import {inject, observer} from "mobx-react";
import {Modal, Button} from "antd";
import {WeaLocaleProvider, WeaDialog} from "ecCom";
import * as authTypes from "../../constants/secondAuthType";
import DynamicPassword from "./DynamicPassword";
import DynamicToken from "./DynamicToken";
import SecondAuthPassword from "./SecondAuthPassword";
import CAAuth from "./CAAuth";
import QYSAuth from "./QYSAuth";
import {doSecondAuth} from "../../apis/secondAuth";
import "./style/index.less";

const warning = Modal.warning;
const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        const {visible, secondAuthStore} = this.props;
        const {secondAuthType} = secondAuthStore;

        const onCancel = this.closeDialog;
        const title = getLabel(501195, "身份验证");
        const buttons = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@h2aaez@confirm`} onClick={this.doSubmit} type="primary">{getLabel(501196, "确定")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@zjw8jb@cancel`} onClick={this.closeDialog}>{getLabel(31129, "取消")}</Button>)
        ];

        const dialogWidth = 600;
        let dialogHeight = 150;
        let style = {width: dialogWidth, height: dialogHeight};
        let dialogParams = {title, style, visible, onCancel, buttons};
        if (secondAuthType == authTypes.CA_AUTH) {
            dialogHeight = 300;    //CA认证，调整窗口高度
            style = {width: dialogWidth, height: dialogHeight};
            dialogParams = {title, style, visible, onCancel};   //不显示按钮
        }

        dialogParams.icon = "icon-coms-workflow";
        dialogParams.iconBgcolor = '#0079DE';

        if (secondAuthType != authTypes.QYS) {
            return (
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@w3lgw8`} {...dialogParams}>
                    <div className="sencond-auth-dialog">
                        {visible && this.renderContent()}
                    </div>
                </WeaDialog>
            );
        } else {
            return this.renderQYSAuth();
        }
    }

    renderContent() {
        const {visible, secondAuthStore} = this.props;
        const {authKey, secondAuthType} = secondAuthStore;
        let content = <div></div>;
        switch (secondAuthType) {
            case authTypes.DYNAMIC_PASSWORD:
                content = <DynamicPassword ecId={`${this && this.props && this.props.ecId || ''}_DynamicPassword@cxrkl6`} needSend={visible} secondAuthStore={secondAuthStore}/>;
                break;
            case authTypes.DYNAMIC_TOKEN:
                content = <DynamicToken ecId={`${this && this.props && this.props.ecId || ''}_DynamicToken@v64dcz`} secondAuthStore={secondAuthStore}/>;
                break;
            case authTypes.SECOND_AUTH_PASSWORD:
                content = <SecondAuthPassword ecId={`${this && this.props && this.props.ecId || ''}_SecondAuthPassword@sr8m38`} secondAuthStore={secondAuthStore}/>;
                break;
            case authTypes.CA_AUTH:
                content = (<CAAuth ecId={`${this && this.props && this.props.ecId || ''}_CAAuth@0ul0ao`}
                    secondAuthStore={secondAuthStore}
                    isShow={visible}
                    onClose={this.props.onClose}
                    onSuccess={this.props.onSuccess}/>);
                break;
            case authTypes.QYS:
                break;
            default:
                break;
        }

        return content;
    }

    renderQYSAuth() {
        return <QYSAuth ecId={`${this && this.props && this.props.ecId || ''}_QYSAuth@19ohes`} {...this.props}/>
    }

    //提交
    doSubmit = () =>  {
        const {secondAuthStore} = this.props;
        const {authCode, secondAuthType} = secondAuthStore;

        if (!authCode) {
            let errorMsg = "";

            switch (secondAuthType) {
                case authTypes.DYNAMIC_PASSWORD:
                    errorMsg = getLabel(501199, "请输入动态密码");
                    break;
                case authTypes.DYNAMIC_TOKEN:
                    errorMsg = getLabel(501200, "请输入动态令牌");
                    break;
                case authTypes.SECOND_AUTH_PASSWORD:
                    errorMsg = getLabel(501201, "请输入二次验证密码");
                    break;
                default:
                    break;
            }

            if (!!errorMsg) {
                Modal.error({content: errorMsg});
            }
        } else {
            const params = {authCode, authType: secondAuthType};
            doSecondAuth(params).then(data => {     //验证二次授权码
                const {api_status, api_errormsg} = data;
                if (api_status) {
                    // this.closeDialog();
                    if (this.props.onSuccess) {
                        this.props.onSuccess.call({}, {...data, secondAuthType});
                    }
                } else {
                    if (!!api_errormsg) {
                        Modal.error({content: api_errormsg});
                    }

                    if (this.props.onFail) {
                        this.props.onFail.call({}, {...data, secondAuthType});
                    }
                }
            })
        }
    }

    //关闭
    closeDialog = () =>  {
        if (this.props.onClose) {
            this.props.onClose.call({});
        }
    }
}
